import { loginByUsername, logout } from '@/api/login'
import { getToken, setToken, removeToken, getUsername, setUsername, removeUsername, getRoles, setRoles, removeRoles, getUserId, removeUserId, setUserId } from '@/utils/auth'

const user = {
    state: {
        token: getToken(),
        username: getUsername(),
        roles: getRoles() ? JSON.parse(getRoles()) : [],
        userId: getUserId(),
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.username = name
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_USERID: (state, userId) => {
            state.userId = userId
        }
    },
    actions: {
        LoginByUsername({ commit }, userInfo) {
            const username = userInfo.username.trim()
            return new Promise((resolve, reject) => {
                loginByUsername(username, userInfo.password).then(response => {
                    const res = response.data;
                    if (res.code != "0") {
                        reject(res)
                    } else {
                        commit('SET_TOKEN', res.data.token || res.data.sessionId)
                        commit('SET_NAME', res.data.username)
                        commit('SET_USERID', res.data.id)
                        let roles = [];
                        res.data.roles.forEach(role => {
                            roles.push(role.id)
                        })
                        commit('SET_ROLES', roles)
                        setToken(res.data.token || res.data.sessionId)
                        setUsername(res.data.username)
                        setRoles(roles)
                        setUserId(res.data.id)
                        resolve()
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        logOut({ commit, dispatch }) {
            return new Promise((resolve, reject) => {
                logout().then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_ROLES', [])
                    commit('SET_MENUS', [])
                    commit('SET_NAME', '')
                    dispatch("delAllViews")
                    removeRoles();
                    removeUsername();
                    removeToken();
                    removeUserId();
                    localStorage.clear()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        logOutFont({ commit, dispatch }) {
            return new Promise((resolve, reject) => {
                commit('SET_TOKEN', '')
                commit('SET_ROLES', [])
                commit('SET_NAME', '')
                commit('SET_MENUS', [])
                dispatch("delAllViews")
                removeRoles();
                removeUsername();
                removeToken();
                removeUserId();
                localStorage.clear()
                resolve()
            })
        }
    }
}

export default user